#include "Head.h"

vector<string> can_item;
map<string,int>fre_item;
vector<string> vec_fre_item1;
map<string,int>fre_item1;
map<string,int>can_item1;
map<string,int>fre_item2;
vector<string>can_item2;
vector<string>can_item3;
map<string,int>fre_item3;
vector<string>can_item4;
map<string,int>fre_item4;
extern int Item_Num;
string filename="F:\\DataSets\\CVS.txt";
void Associate_init()
{
	double sup;
	double con;
	int k;
	double d;
	cout<<"input the support and the confidence"<<endl;
	cin>>sup>>con;
	cout<<"input the no of top rules"<<endl;
	cin>>k;
	cout<<"input the iterative decrease support"<<endl;
	cin>>d;
	Associate Ass(sup,con,k,d);
	Ass.getinfos();
	int select;
	cout<<"input the algorithms to generate the rules,1--Apriori and 2--Fp-Growth \n"
		"or 0 to exit"<<endl;
	cin>>select;
	switch(select)
	{
	case 1:
		Ass.Apriori();
		break;
	case 2:
		Ass.FP_Growth();
		break;
	case 0:
		exit(1);
	}

}

map<string,int> &Associate::Candidate_item1(const vector<string> & vec)
{
	
	for(vector<string>::const_iterator iter=vec.begin();iter!=vec.end();iter++)
		can_item1[*iter]++;

	return can_item1;

}

void Associate::Apriori()
{
	vector<string> item=File(filename);
	map<string,int> temp_item1=Candidate_item1(item);

	for (map<string,int>::iterator iter=temp_item1.begin();iter!=temp_item1.end();iter++)
	{
		if (double(iter->second)/Item_Num>=support)
		{
			fre_item1.insert(make_pair(iter->first,iter->second));
		}
	}

	cout<<"The fre_item1 is"<<endl;

	for(auto iter=fre_item1.begin();iter!=fre_item1.end();iter++)
	{
		cout<<iter->first<<" "<<iter->second<<endl;
		vec_fre_item1.push_back(iter->first);
	} 
	for (auto iter=vec_fre_item1.begin();iter!=vec_fre_item1.end();iter++)
	{
		cout<<*iter<<endl;
	}
	can_item2=AprioriGen(vec_fre_item1,2);
	fre_item2=ScanFile(can_item2,filename);
	can_item3=AprioriGen(can_item2,3);
	fre_item3=ScanFile(can_item3,filename);
}


vector<string> & Associate::AprioriGen(vector<string> &vec_item,int k)
{
	bool flag=true;
	can_item.clear();
	vector<string> temps1;
	vector<string> temps2;
	set<string>set_temp1;
	set<string>set_temp2;
	set<string>set_temp;

	string s;
	
	for (auto iter1=vec_item.begin();iter1!=vec_item.end();iter1++)
	{
		temps1.clear();
		set_temp1.clear();
		string temp1=string(*iter1);
	    temps1=Seperateword(temp1);
		temps1.insert(temps1.begin()," ");
		set_temp1.insert(temps1.begin(),temps1.end());
		for (auto iter2=iter1+1;iter2!=vec_item.end();iter2++)
		{
			s.clear();
			set_temp.clear();
			temps2.clear();
			set_temp2.clear();
			string temp2=string(*iter2);
		    temps2=Seperateword(temp2);
			temps2.insert(temps2.begin()," ");
			set_temp2.insert(temps2.begin(),temps2.end());
			for (int i=0;i<=k-2;i++)
			{
				if (temps1[i]!=temps2[i])
				{
					flag=false;
				}
			}
			if(flag)
			{
			   set_union(set_temp1.begin(),set_temp1.end(),set_temp2.begin(),set_temp2.end(),inserter(set_temp,set_temp.begin()));
			}
			for (auto iter=set_temp.begin();iter!=set_temp.end();iter++)
			{
				s+=*iter+",";
	    	}
			s=s.substr(0,s.length()-1);
			can_item.push_back(s);
		}	
	}
	return can_item;
}


void Associate::FP_Growth()
{
	FP_Tree * root=new FP_Tree;

	root->CreateTree(root);
}

map<string,int>& Associate::ScanFile(vector<string> & vec,const string & file)
{
	vector<string> temp;
	bool flag;
	string line;
	ifstream in(file);
	if(!in.is_open())
		exit(1);
	while (!in.eof())
	{
		in>>line;

		for (auto iter=vec.begin();iter!=vec.end();iter++)
		{
			temp.clear();

			temp=Seperateword(*iter);

			for (auto iters=temp.begin();iters!=temp.end();iters++)
			{
				if (line.find(*iters)!=string::npos)
				{
					flag=true;
				}
			}

			if (flag)
			{
				fre_item[*iter]++;
			}

			flag=false;
		}
	}

	cout<<"The Fre _Item is"<<endl;

	for (auto iter=fre_item.begin();iter!=fre_item.end();iter++)
	{
		cout<<iter->first<<" "<<iter->second<<endl;
	}

	cout<<endl;
	return fre_item;


}


